package com.lyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.dto.DishDto;
import com.lyl.entity.Dish;
import com.lyl.entity.DishFlavor;
import com.lyl.mapper.DishMapper;
import com.lyl.service.DishFlavorService;
import com.lyl.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李亚龙
 * @version 1.0
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;//注入菜品口味的服务层对象
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);

        //保存菜品口味数据到菜品口味表之前，设置关联菜品的id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach((flavor) -> flavor.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过菜品id进行查询菜品信息和对应的口味信息，用于修改回显
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        //构造条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        //查询dishFlavor信息
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);

        //新建dishDto对象
        DishDto dishDto = new DishDto();
        //对对象进行拷贝属性和赋值
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //请理dish_flavor表中对应dish_id的信息
        Long dish_id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish_id);
        dishFlavorService.remove(queryWrapper);
        //增添新的修改的信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach((item) -> {item.setDishId(dish_id);});
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        //先删除dish_flavor表中的口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
//        ids.forEach((id)->{
//            queryWrapper.eq(DishFlavor::getDishId,id);
//            dishFlavorService.remove(queryWrapper);
//            queryWrapper.clear();
//        });
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);

        //删除dish表中的菜品
        super.removeBatchByIds(ids);

    }
}
